https://azure.microsoft.com/ko-kr/services/cognitive-services/
- 디렉토리 : https://azure.microsoft.com/ko-kr/services/cognitive-services/directory/vision/
- 메모리 사용용량은 4MB
- 외부라이브러리 사용시 제약 있을 수 있다.
from tqdm import tqdm_notebook
# MS Azure Cognitive Services
bpc.Figure(bpc.AZN_FC_FIG_01)
# 사용자 API
bpc.Figure(bpc.AZN_FC_FIG_02)
이 자습서를 사용하려면 다음을 수행해야 합니다.
- Python 2.7+ 또는 Python 3.5+ 중 하나를 설치합니다.
- pip를 설치합니다.
- 다음과 같이 Face API용 Python SDK를 설치합니다.
bash pip install cognitive_face- Microsoft Cognitive Services에 대한 구독 키를 구합니다. 이 자습서의 주 또는 보조 키를 사용할 수 있습니다. (Face API를 사용하려면 유효한 구독 키가 있어야 합니다.)
Face
markdown
사진에서 얼굴을 감지, 식별, 분석, 구성 및 태그 지정
30,000개의 트랜잭션, 분당 20개.
엔드포인트: https://westcentralus.api.cognitive.microsoft.com/face/v1.0
키 1: 4f7ec58767f64cd391de86fbb7f8b410
키 2: 54ca95aea4784eae8aa6c1764ffdd241
# python code
import cognitive_face as CF
KEY = '<Subscription Key>' # Replace with a valid subscription key (keeping the quotes in place).
CF.Key.set(KEY)
BASE_URL = 'https://westus.api.cognitive.microsoft.com/face/v1.0/' # Replace with your regional Base URL
CF.BaseUrl.set(BASE_URL)
# You can use this example JPG or replace the URL below with your own URL to a JPEG image.
img_url = 'https://raw.githubusercontent.com/Microsoft/Cognitive-Face-Windows/master/Data/detection1.jpg'
faces = CF.face.detect(img_url)
print(faces)
다음은 예제 결과입니다. 감지된 얼굴의 list입니다. 목록의 각 항목은 dict 인스턴스입니다.
여기서 faceId는 감지된 얼굴의 고유 ID이고 faceRectangle은 감지된 얼굴 위치를 설명합니다. 얼굴 ID는 24시간 후에 만료됩니다.
[실행결과]
[{u'faceId': u'68a0f8cf-9dba-4a25-afb3-f9cdf57cca51', u'faceRectangle': {u'width': 89, u'top': 66, u'height': 89, u'left': 446}}]
# Resource Management, Keys
# endpoint = 'https://westcentralus.api.cognitive.microsoft.com/face/v1.0'
# subscription_key1 = '4f7ec58767f64cd391de86fbb7f8b410'
# subscription_key2 = '54ca95aea4784eae8aa6c1764ffdd241'
# base_url = endpoint
# subscription_key = subscription_key1
# 자신의 사용자 API를 등록
base_url = 'https://westcentralus.api.cognitive.microsoft.com/face/v1.0'
subscription_key = '********************************'
import cognitive_face as CF
KEY = subscription_key # Replace with a valid subscription key (keeping the quotes in place).
CF.Key.set(KEY)
BASE_URL = 'https://westus.api.cognitive.microsoft.com/face/v1.0/' # Replace with your regional Base URL
CF.BaseUrl.set(endpoint)
# You can use this example JPG or replace the URL below with your own URL to a JPEG image.
img_url = 'https://raw.githubusercontent.com/Microsoft/Cognitive-Face-Windows/master/Data/detection1.jpg'
faces = CF.face.detect(img_url)
print(faces)
이전 명령에서 얻은 json 좌표를 사용하여 이미지에 각 얼굴을 시각적으로 나타내는 사각형을 그릴 수 있습니다.
pip install Pillow를 수행하여 PIL 이미징 모듈을 사용해야 합니다. 파일 맨 위에 다음을 추가합니다.
# python code
import requests
from io import BytesIO
from PIL import Image, ImageDraw
#Convert width height to a point in a rectangle
def getRectangle(faceDictionary):
rect = faceDictionary['faceRectangle']
left = rect['left']
top = rect['top']
bottom = left + rect['height']
right = top + rect['width']
return ((left, top), (bottom, right))
#Download the image from the url
response = requests.get(img_url)
img = Image.open(BytesIO(response.content))
#For each face returned use the face rectangle and draw a red box.
draw = ImageDraw.Draw(img)
for face in faces:
draw.rectangle(getRectangle(face), outline='red')
#Display the image in the users default image browser.
img.show()
import requests
from io import BytesIO
from PIL import Image, ImageDraw
#Convert width height to a point in a rectangle
def getRectangle(faceDictionary):
rect = faceDictionary['faceRectangle']
left = rect['left']
top = rect['top']
bottom = left + rect['height']
right = top + rect['width']
return ((left, top), (bottom, right))
#Download the image from the url
response = requests.get(img_url)
img = Image.open(BytesIO(response.content))
#For each face returned use the face rectangle and draw a red box.
draw = ImageDraw.Draw(img)
for face in faces:
draw.rectangle(getRectangle(face), outline='red')
#Display the image in the users default image browser.
img.show()
img
Face API를 추가로 탐색하는 데 도움이 되도록 이 자습서는 GUI 예제를 제공합니다.
이를 실행하려면 먼저 wxPython을 설치한 후 아래 명령을 실행합니다.
[bash]
git clone https://github.com/Microsoft/Cognitive-Face-Python.git
cd Cognitive-Face-Python
python sample
Face
markdown
Detect, identify, analyze, organize, and tag faces in photos
30,000 transactions, 20 per minute.
Endpoint: https://westcentralus.api.cognitive.microsoft.com/face/v1.0
Key 1: c7415e098dc045698a07d4171b69ba46
Key 2: 85f3e691e91c492fa9c8cc68d35773fb
시각 : Face, 이미지의 감정 인식
- 이미지 처리 알고리즘을 통해 사진을 스마트하게 인식, 캡션 작성, 조정
- 분노, 경멸, 역겨움, 두려움, 행복, 중립, 슬픔, 놀람
- 분노, 경멸, 역겨움, 두려움, 행복, 중립, 슬픔, 놀람
시각 : Face, 이미지의 얼굴 감지
- 이미지 처리 알고리즘을 통해 사진을 스마트하게 인식, 캡션 작성, 조정
- 이미지에서 얼굴부분만 탐색하여 사각형으로 시각적으로 감지
# 엔드포인트: https://westcentralus.api.cognitive.microsoft.com/face/v1.0
# 키 1: 4f7ec58767f64cd391de86fbb7f8b410
# 키 2: 54ca95aea4784eae8aa6c1764ffdd241
endpoint = 'https://westcentralus.api.cognitive.microsoft.com/face/v1.0'
subscription_key1 = '4f7ec58767f64cd391de86fbb7f8b410'
subscription_key2 = '54ca95aea4784eae8aa6c1764ffdd241'
base_url = endpoint
subscription_key = subscription_key1
from io import BytesIO
from PIL import Image, ImageDraw, ImageFont
import requests
# 구글링을 통해 이미지 url 복사
image_url = 'http://harpersbazaar.co.kr/wp-content/uploads/2018/05/bazaar-2018-06-celeb-106-008.jpg'
face_image = requests.get(image_url)
face_image
type(face_image)
img = Image.open(BytesIO(face_image.content))
img
import cognitive_face as CF
KEY = subscription_key # Replace with a valid subscription key (keeping the quotes in place).
CF.Key.set(KEY)
BASE_URL = 'https://westus.api.cognitive.microsoft.com/face/v1.0/' # Replace with your regional Base URL
CF.BaseUrl.set(endpoint)
faces = CF.face.detect(image_url)
faces
draw = ImageDraw.Draw(img)
len(faces)
face = faces[0]
rect = face['faceRectangle']
left = rect['left']
top = rect['top']
width = rect['width']
height = rect['height']
right = left + width
bottom = top + height
print(' left, top = {l}, {t} \n bottom, right = {b}, {r} \n width, height = {w}, {h}'.format(
l = left,
t = top,
b = bottom,
r = right,
w = width,
h = height
))
draw.rectangle(
((left, top), (right, bottom)),
outline = 'red'
)
img
import cognitive_face as CF
KEY = subscription_key # Replace with a valid subscription key (keeping the quotes in place).
CF.Key.set(KEY)
BASE_URL = 'https://westus.api.cognitive.microsoft.com/face/v1.0/' # Replace with your regional Base URL
CF.BaseUrl.set(endpoint)
# 효린
image_url = 'http://harpersbazaar.co.kr/wp-content/uploads/2018/05/bazaar-2018-06-celeb-106-008.jpg'
faces = CF.face.detect(image_url)
print(faces)
# 엔드포인트: https://westcentralus.api.cognitive.microsoft.com/face/v1.0
# 키 1: 4f7ec58767f64cd391de86fbb7f8b410
# 키 2: 54ca95aea4784eae8aa6c1764ffdd241
endpoint = 'https://westcentralus.api.cognitive.microsoft.com/face/v1.0'
subscription_key1 = '4f7ec58767f64cd391de86fbb7f8b410'
subscription_key2 = '54ca95aea4784eae8aa6c1764ffdd241'
base_url = endpoint
subscription_key = subscription_key1
from io import BytesIO
from PIL import Image, ImageDraw, ImageFont
import requests
import cognitive_face as CF
def downloadImage(image_url):
face_image = requests.get(image_url)
img = Image.open(BytesIO(face_image.content))
return img
def detectFaces(image_url):
KEY = subscription_key # Replace with a valid subscription key (keeping the quotes in place).
CF.Key.set(KEY)
BASE_URL = 'https://westus.api.cognitive.microsoft.com/face/v1.0/' # Replace with your regional Base URL
CF.BaseUrl.set(endpoint)
faces = CF.face.detect(image_url)
return faces
def drawFaceRectangle(faces):
idx = 1
for face in faces:
rect = face['faceRectangle']
left = rect['left']
top = rect['top']
width = rect['width']
height = rect['height']
right = left + width
bottom = top + height
draw.rectangle(
((left, top), (right, bottom)),
outline = 'red'
)
if len(faces)>1 :
if idx > 1:
print('-'*50)
print(' [Face {}]'.format(idx))
print(' - left, top = {l}, {t} \n - bottom, right = {b}, {r} \n - width, height = {w}, {h}'.format(
l = left,
t = top,
b = bottom,
r = right,
w = width,
h = height
))
idx += 1
return img
image_url = 'http://harpersbazaar.co.kr/wp-content/uploads/2018/05/bazaar-2018-06-celeb-106-008.jpg'
# 이미지를 다운받은 후, 원래 이미지는 백업
img = downloadImage(image_url)
original_img = img.copy()
original_img
faces = detectFaces(image_url)
faces
draw = ImageDraw.Draw(img)
detection_img = drawFaceRectangle(faces)
detection_img
faces = CF.face.detect(image_url)
print(faces)
draw = ImageDraw.Draw(img)
draw
# 효린 & 소유
image_url = 'http://img.tf.co.kr/article/home/2015/09/02/201517841441157761.jpg'
img = downloadImage(image_url)
sistar_img = img.copy()
sistar_img
faces = detectFaces(image_url)
draw = ImageDraw.Draw(img)
detection_img = drawFaceRectangle(faces)
detection_img
- 얼굴 이미지가 작은 경우 인식률이 떨어진다.
- 해상도가 떨어지는 경우도 인식률이 떨어진다.
# 씨스타 전부
image_url = 'http://photo.jtbc.joins.com/news/2015/05/28/20150528161815685.jpg'
image_url = 'https://img.sbs.co.kr/newimg/news/20170825/201086539_1280.jpg'
image_url = 'https://kenh14cdn.com/2017/170525musiksistar1-1495719681082.jpg'
img = downloadImage(image_url)
snsd_img = img.copy()
snsd_img
faces = detectFaces(image_url)
draw = ImageDraw.Draw(img)
detection_img = drawFaceRectangle(faces)
detection_img